<!DOCTYPE HTML>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1" /> 
    <title> - 天地维杰网</title>
    <meta name="keywords" content="系统架构,shutdown,不与天斗,Domino,博客,程序员,架构师,笔记,技术,分享,java,Redis">
    
    <meta property="og:title" content="">
    <meta property="og:site_name" content="天地维杰网">
    <meta property="og:image" content="/img/author.jpg"> 
    <meta name="title" content=" - 天地维杰网" />
    <meta name="description" content="天地维杰网 | 博客 | 软件 | 架构 | Java "> 
    <link rel="shortcut icon" href="http://www.shutdown.cn/img/favicon.ico" />
    <link rel="apple-touch-icon" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link rel="apple-touch-icon-precomposed" href="http://www.shutdown.cn/img/apple-touch-icon.png" />
    <link href="http://www.shutdown.cn/js/vendor/font-awesome/css/font-awesome.min.css?v=4.6.2" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.css?v=2.1.5" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/main.css" rel="stylesheet" type="text/css" />
    <link href="http://www.shutdown.cn/css/syntax.css" rel="stylesheet" type="text/css" />
    <script type="text/javascript" id="hexo.configuration">
  var NexT = window.NexT || {};
  var CONFIG = {
    scheme: 'Pisces',
    sidebar: {"position":"left","display":"post"},
     fancybox: true, 
    motion: true
  };
</script>
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=ca-pub-7826003325059020" crossorigin="anonymous"></script>
</head>
<body itemscope itemtype="http://schema.org/WebPage" lang="zh-Hans">
<div class="container one-collumn sidebar-position-left page-home  ">
    <div class="headband"></div>

    <header id="header" class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"> <div class="site-meta  custom-logo ">

  <div class="custom-logo-site-title">
    <a href="http://www.shutdown.cn"  class="brand" rel="start">
      <span class="logo-line-before"><i></i></span>
      <span class="site-title">天地维杰网</span>
      <span class="logo-line-after"><i></i></span>
    </a>
  </div>
  <p class="site-subtitle">人如秋鸿来有信，事若春梦了无痕</p>
</div>

<div class="site-nav-toggle">
  <button>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
    <span class="btn-bar"></span>
  </button>
</div>

<nav class="site-nav">
    <ul id="menu" class="menu">
      
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-home"></i> <br />首页
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/redis/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-full"></i> <br />Redis
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/java/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-coffee"></i> <br />java
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/linux/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-linux"></i> <br />linux
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/daily/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bug"></i> <br />日常问题
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/spring/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-child"></i> <br />Spring和Springboot
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/mac/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-fire"></i> <br />Mac相关
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/middleware/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-gavel"></i> <br />中间件
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jiagou/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-rocket"></i> <br />架构
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/python/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-ship"></i> <br />python
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/front/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-bolt"></i> <br />前端
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/jvm/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-balance-scale"></i> <br />jvm
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/c/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-battery-empty"></i> <br />c语言
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/categories/web3/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-web3"></i> <br />web3
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/post/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-archive"></i> <br />归档
          </a>
        </li>
      
        <li class="menu-item ">
          <a href="http://www.shutdown.cn/about/" rel="section">
              <i class="menu-item-icon fa fa-fw fa-user"></i> <br />关于
          </a>
        </li>
      
      <li class="menu-item menu-item-search">
        <a href="javascript:;" class="popup-trigger"> <i class="menu-item-icon fa fa-search fa-fw"></i> <br /> 搜索</a>
      </li>
    </ul>
    <div class="site-search">
      <div class="popup">
 <span class="search-icon fa fa-search"></span>
 <input type="text" id="local-search-input">
 <div id="local-search-result"></div>
 <span class="popup-btn-close">close</span>
</div>

    </div>
</nav>

 </div>
    </header>

    <main id="main" class="main">
      <div class="main-inner">
        <div class="content-wrap">
          <div id="content" class="content">
            
<section id="posts" class="posts-expand">
  <article class="post post-type-normal " itemscope itemtype="http://schema.org/Article">
    <header class="post-header">
      <h1 class="post-title" itemprop="name headline">
        <a class="post-title-link" href="http://www.shutdown.cn/post/redission/" itemprop="url">
        
        </a>
      </h1>
      <div class="post-meta">
      <span class="post-time">
<span class="post-meta-item-icon">
    <i class="fa fa-calendar-o"></i>
</span>
<span class="post-meta-item-text">时间：</span>
<time itemprop="dateCreated" datetime="2016-03-22T13:04:35+08:00" content="0001-01-01">
    0001-01-01
</time>
</span> 
      
      
       <span>
&nbsp; | &nbsp;
<span class="post-meta-item-icon">
    <i class="fa fa-eye"></i>
</span>
<span class="post-meta-item-text">阅读：</span>
<span class="leancloud-visitors-count">719 字 ~4分钟</span>
</span>
      </div>
    </header>
    <div class="post-body" itemprop="articleBody">
    

    

<p>首先关于redisson的介绍，这里就不搬运了，贴一下github原地址：</p>

<ol>
<li><a href="https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fredisson%2Fredisson%2Fwiki%2F1.-%E6%A6%82%E8%BF%B0">概述</a></li>
</ol>

<p>由于我这里只是简单使用了redisson的 <code>分布式锁</code> 的功能，这里仅记录下锁的简单使用。</p>

<p>官方文档：<a href="https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fredisson%2Fredisson%2Fwiki%2F8.-%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E5%92%8C%E5%90%8C%E6%AD%A5%E5%99%A8">8.分布式锁和同步器</a></p>

<p>此次所用锁为可重入锁</p>

<h3 id="8-1-可重入锁-reentrant-lock">8.1. 可重入锁（Reentrant Lock）</h3>

<p>基于Redis的Redisson分布式可重入锁<a href="https://link.juejin.cn?target=http%3A%2F%2Fstatic.javadoc.io%2Forg.redisson%2Fredisson%2F3.10.0%2Forg%2Fredisson%2Fapi%2FRLock.html"><code>RLock</code></a> Java对象实现了<code>java.util.concurrent.locks.Lock</code>接口。同时还提供了<a href="https://link.juejin.cn?target=http%3A%2F%2Fstatic.javadoc.io%2Forg.redisson%2Fredisson%2F3.10.0%2Forg%2Fredisson%2Fapi%2FRLockAsync.html">异步（Async）</a>、<a href="https://link.juejin.cn?target=http%3A%2F%2Fstatic.javadoc.io%2Forg.redisson%2Fredisson%2F3.10.0%2Forg%2Fredisson%2Fapi%2FRLockReactive.html">反射式（Reactive）</a>和<a href="https://link.juejin.cn?target=http%3A%2F%2Fstatic.javadoc.io%2Forg.redisson%2Fredisson%2F3.10.0%2Forg%2Fredisson%2Fapi%2FRLockRx.html">RxJava2标准</a>的接口。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js"><span style="color:#000">RLock</span> <span style="color:#000">lock</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">redisson</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getLock</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;anyLock&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">// 最常见的使用方法
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000">复制代码</span>
</code></pre></div>
<p>大家都知道，如果负责储存这个分布式锁的Redisson节点宕机以后，而且这个锁正好处于锁住的状态时，这个锁会出现锁死的状态。为了避免这种情况的发生，Redisson内部提供了一个监控锁的看门狗，它的作用是在Redisson实例被关闭前，不断的延长锁的有效期。默认情况下，看门狗的检查锁的超时时间是30秒钟，也可以通过修改<a href="https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fredisson%2Fredisson%2Fwiki%2F2.-%E9%85%8D%E7%BD%AE%E6%96%B9%E6%B3%95%23lockwatchdogtimeout%E7%9B%91%E6%8E%A7%E9%94%81%E7%9A%84%E7%9C%8B%E9%97%A8%E7%8B%97%E8%B6%85%E6%97%B6%E5%8D%95%E4%BD%8D%E6%AF%AB%E7%A7%92">Config.lockWatchdogTimeout</a>来另行指定。</p>

<p>另外Redisson还通过加锁的方法提供了<code>leaseTime</code>的参数来指定加锁的时间。超过这个时间后锁便自动解开了。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js"><span style="color:#8f5902;font-style:italic">// 加锁以后10秒钟自动解锁
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">// 无需调用unlock方法手动解锁
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">10</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">TimeUnit</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SECONDS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>

<span style="color:#8f5902;font-style:italic">// 尝试加锁，最多等待100秒，上锁以后10秒自动解锁
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#204a87;font-weight:bold">boolean</span> <span style="color:#000">res</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">tryLock</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">100</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">10</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">TimeUnit</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SECONDS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">res</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
   <span style="color:#204a87;font-weight:bold">try</span> <span style="color:#000;font-weight:bold">{</span>
     <span style="color:#000;font-weight:bold">...</span>
   <span style="color:#000;font-weight:bold">}</span> <span style="color:#204a87;font-weight:bold">finally</span> <span style="color:#000;font-weight:bold">{</span>
       <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">unlock</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
   <span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">}</span>
<span style="color:#000">复制代码</span>
</code></pre></div>
<p>Redisson同时还为分布式锁提供了异步执行的相关方法：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js"><span style="color:#000">RLock</span> <span style="color:#000">lock</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">redisson</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getLock</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;anyLock&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lockAsync</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">lockAsync</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">10</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">TimeUnit</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SECONDS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000">Future</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#204a87">Boolean</span><span style="color:#ce5c00;font-weight:bold">&gt;</span> <span style="color:#000">res</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">tryLockAsync</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">100</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">10</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">TimeUnit</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SECONDS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#000">复制代码</span>
</code></pre></div>
<p><code>RLock</code>对象完全符合Java的Lock规范。也就是说只有拥有锁的进程才能解锁，其他进程解锁则会抛出<code>IllegalMonitorStateException</code>错误。但是如果遇到需要其他进程也能解锁的情况，请使用<a href="https://link.juejin.cn?target=https%3A%2F%2Fgithub.com%2Fredisson%2Fredisson%2Fwiki%2F8.-%E5%88%86%E5%B8%83%E5%BC%8F%E9%94%81%E5%92%8C%E5%90%8C%E6%AD%A5%E5%99%A8%2386-%E4%BF%A1%E5%8F%B7%E9%87%8Fsemaphore">分布式信号量<code>Semaphore</code></a> 对象.</p>

<p>首先springboot整合redisson需要引入redisson的依赖：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js"><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">dependency</span><span style="color:#ce5c00;font-weight:bold">&gt;</span>
    <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">groupId</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">redisson</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#a40000">/</span><span style="color:#a40000">g</span><span style="color:#a40000">r</span><span style="color:#a40000">o</span><span style="color:#a40000">u</span><span style="color:#a40000">p</span><span style="color:#a40000">I</span><span style="color:#a40000">d</span><span style="color:#a40000">&gt;</span>
    <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">artifactId</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#000">redisson</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">spring</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">boot</span><span style="color:#ce5c00;font-weight:bold">-</span><span style="color:#000">starter</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#a40000">/</span><span style="color:#a40000">a</span><span style="color:#a40000">r</span><span style="color:#a40000">t</span><span style="color:#a40000">i</span><span style="color:#a40000">f</span><span style="color:#a40000">a</span><span style="color:#a40000">c</span><span style="color:#a40000">t</span><span style="color:#a40000">I</span><span style="color:#a40000">d</span><span style="color:#a40000">&gt;</span>
    <span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#000">version</span><span style="color:#ce5c00;font-weight:bold">&gt;</span><span style="color:#0000cf;font-weight:bold">3.13</span><span style="color:#0000cf;font-weight:bold">.6</span><span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#a40000">/</span><span style="color:#a40000">v</span><span style="color:#a40000">e</span><span style="color:#a40000">r</span><span style="color:#a40000">s</span><span style="color:#a40000">i</span><span style="color:#a40000">o</span><span style="color:#a40000">n</span><span style="color:#a40000">&gt;</span>
<span style="color:#ce5c00;font-weight:bold">&lt;</span><span style="color:#a40000">/</span><span style="color:#a40000">d</span><span style="color:#a40000">e</span><span style="color:#a40000">p</span><span style="color:#a40000">e</span><span style="color:#a40000">n</span><span style="color:#a40000">d</span><span style="color:#a40000">e</span><span style="color:#a40000">n</span><span style="color:#a40000">c</span><span style="color:#a40000">y</span><span style="color:#a40000">&gt;</span>
<span style="color:#000">复制代码</span>
</code></pre></div>
<p>亲测以上版本，配合springboot：<code>2.2.5.RELEASE</code>版本，正常使用。</p>

<p>单机版redis的配置文件跟原来springboot集成redis一样。</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">spring:
    # Redis配置
    redis:
      timeout: 6000 # 连接超时时长（毫秒）
      password: huauN@2021
      database: 0
      host: 192.168.104.64
      port: 6379
      #cluster:
        #max-redirects: 3  # 获取失败 最大重定向次数
        #nodes:
          #- 192.168.104.101:6379
      lettuce:
        pool:
          max-active: 1024 # 连接池最大连接数（默认为8，-1表示无限制 如果pool已经分配了超过max_active个jedis实例，则此时pool为耗尽）
          max-wait: 10000 #最大等待连接时间，单位毫秒 默认为-1，表示永不超时，超时会抛出JedisConnectionException
          max-idle: 10
          min-idle: 5
复制代码</code></pre></div>
<p>Redis配置映射类 <code>RedisConfigProperties.java</code></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;

/**
 * Redis配置映射类
 *
 * @author linmengmeng
 * @date 2021-03-11
 **/

@Component
@ConfigurationProperties(prefix = &#34;spring.redis&#34;)
public class RedisConfigProperties {
    private Integer timeout;
    private Integer database;
    private Integer port;
    private String host;
    private String password;
    private cluster cluster;

    public static class cluster {
        private List&lt;String&gt; nodes;

        public List&lt;String&gt; getNodes() {
            return nodes;
        }

        public void setNodes(List&lt;String&gt; nodes) {
            this.nodes = nodes;
        }
    }

    public Integer getTimeout() {
        return timeout;
    }

    public void setTimeout(Integer timeout) {
        this.timeout = timeout;
    }

    public Integer getDatabase() {
        return database;
    }

    public void setDatabase(Integer database) {
        this.database = database;
    }

    public Integer getPort() {
        return port;
    }

    public void setPort(Integer port) {
        this.port = port;
    }

    public String getHost() {
        return host;
    }

    public void setHost(String host) {
        this.host = host;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public RedisConfigProperties.cluster getCluster() {
        return cluster;
    }

    public void setCluster(RedisConfigProperties.cluster cluster) {
        this.cluster = cluster;
    }
}
复制代码</code></pre></div>
<p>添加自动装配类：<code>RedissonConfig.java</code></p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-js" data-lang="js"><span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">gc</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">cnnvd</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">config</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">properties</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">RedisConfigProperties</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">redisson</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Redisson</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">redisson</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">config</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Config</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">springframework</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">beans</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">factory</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">annotation</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Autowired</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">springframework</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">context</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">annotation</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Bean</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">springframework</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">context</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">annotation</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Configuration</span><span style="color:#000;font-weight:bold">;</span>

<span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic"> * @author linmengmeng
</span><span style="color:#8f5902;font-style:italic"> * @author 2021-08-30
</span><span style="color:#8f5902;font-style:italic"> */</span>
<span style="color:#a40000">@</span><span style="color:#000">Configuration</span>
<span style="color:#204a87;font-weight:bold">public</span> <span style="color:#204a87;font-weight:bold">class</span> <span style="color:#000">RedissonConfig</span> <span style="color:#000;font-weight:bold">{</span>

    <span style="color:#a40000">@</span><span style="color:#000">Autowired</span>
    <span style="color:#204a87;font-weight:bold">private</span> <span style="color:#000">RedisConfigProperties</span> <span style="color:#000">redisConfigProperties</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">     * redis://host:port
</span><span style="color:#8f5902;font-style:italic">     */</span>
    <span style="color:#204a87;font-weight:bold">private</span> <span style="color:#204a87;font-weight:bold">static</span> <span style="color:#204a87;font-weight:bold">final</span> <span style="color:#204a87">String</span> <span style="color:#000">REDIS_ADDRESS</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#4e9a06">&#34;redis://%s:%s&#34;</span><span style="color:#000;font-weight:bold">;</span>

<span style="color:#8f5902;font-style:italic">//    /**
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//     * 集群模式-添加redisson的bean
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//     * @return
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//     */
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//    @Bean
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//    public Redisson redisson() {
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        //redisson版本是3.5，集群的ip前面要加上“redis://”，不然会报错，3.2版本可不加
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        List&lt;String&gt; clusterNodes = new ArrayList&lt;&gt;();
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        for (int i = 0; i &lt; redisConfigProperties.getCluster().getNodes().size(); i++) {
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//            clusterNodes.add(&#34;redis://&#34; + redisConfigProperties.getCluster().getNodes().get(i));
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        }
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        Config config = new Config();
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        ClusterServersConfig clusterServersConfig = config.useClusterServers()
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//                .addNodeAddress(clusterNodes.toArray(new String[clusterNodes.size()]));
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        clusterServersConfig.setPassword(redisConfigProperties.getPassword());//设置密码
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        return (Redisson) Redisson.create(config);
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//    }
</span><span style="color:#8f5902;font-style:italic"></span>
    <span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic">     * Redisson单机模式
</span><span style="color:#8f5902;font-style:italic">     * @return
</span><span style="color:#8f5902;font-style:italic">     */</span>
    <span style="color:#a40000">@</span><span style="color:#000">Bean</span>
    <span style="color:#204a87;font-weight:bold">public</span> <span style="color:#000">Redisson</span> <span style="color:#000">RedissonConfig</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">Config</span> <span style="color:#000">config</span> <span style="color:#ce5c00;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">new</span> <span style="color:#000">Config</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">//        config.useSingleServer().setAddress(&#34;redis://localhost:6379&#34;).setDatabase(redisConfigProperties.getDatabase());
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">config</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">useSingleServer</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">setAddress</span><span style="color:#000;font-weight:bold">(</span><span style="color:#204a87">String</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">format</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">REDIS_ADDRESS</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">redisConfigProperties</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getHost</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">redisConfigProperties</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getPort</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span>
                <span style="color:#000;font-weight:bold">.</span><span style="color:#000">setDatabase</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">redisConfigProperties</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getDatabase</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span>
                <span style="color:#000;font-weight:bold">.</span><span style="color:#000">setPassword</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">redisConfigProperties</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getPassword</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span><span style="color:#8f5902;font-style:italic">// 没有密码可以不设置
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">Redisson</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000">Redisson</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">create</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">config</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>
<span style="color:#000;font-weight:bold">}</span>
<span style="color:#000">复制代码</span>
</code></pre></div>
<p>创建测试接口测试分布式锁：</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-go" data-lang="go"><span style="color:#204a87;font-weight:bold">package</span> <span style="color:#000">gc</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">cnnvd</span><span style="color:#000;font-weight:bold">;</span>

<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">gc</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">cnnvd</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">framework</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">common</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">api</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ApiResult</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">lombok</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">extern</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">slf4j</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Slf4j</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">redisson</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Redisson</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">redisson</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">api</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">RLock</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">springframework</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">beans</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">factory</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">annotation</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Autowired</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">springframework</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">data</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">redis</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">core</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">RedisTemplate</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">springframework</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">web</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">bind</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">annotation</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">PostMapping</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">springframework</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">web</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">bind</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">annotation</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">RequestMapping</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">org</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">springframework</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">web</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">bind</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">annotation</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">RestController</span><span style="color:#000;font-weight:bold">;</span>

<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">util</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">concurrent</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ExecutionException</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">util</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">concurrent</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">Future</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#204a87;font-weight:bold">import</span> <span style="color:#000">java</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">util</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">concurrent</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">TimeUnit</span><span style="color:#000;font-weight:bold">;</span>

<span style="color:#8f5902;font-style:italic">/**
</span><span style="color:#8f5902;font-style:italic"> * @Auther linmengmeng
</span><span style="color:#8f5902;font-style:italic"> * @Date 2021-09-01 15:37
</span><span style="color:#8f5902;font-style:italic"> */</span>
<span style="color:#a40000">@</span><span style="color:#000">Slf4j</span>
<span style="color:#a40000">@</span><span style="color:#000">RestController</span>
<span style="color:#a40000">@</span><span style="color:#000">RequestMapping</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;/tourist&#34;</span><span style="color:#000;font-weight:bold">)</span>
<span style="color:#000">public</span> <span style="color:#000">class</span> <span style="color:#000">TestRedissonLockController</span> <span style="color:#000;font-weight:bold">{</span>

    <span style="color:#000">private</span> <span style="color:#000">static</span> <span style="color:#000">String</span> <span style="color:#000">FORMAT_LOCKKEY</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#4e9a06">&#34;testLockKey:%s&#34;</span><span style="color:#000;font-weight:bold">;</span><span style="color:#8f5902;font-style:italic">//分布式锁的key
</span><span style="color:#8f5902;font-style:italic"></span>
    <span style="color:#a40000">@</span><span style="color:#000">Autowired</span>
    <span style="color:#000">private</span> <span style="color:#000">RedisTemplate</span> <span style="color:#000">redisTemplate</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#a40000">@</span><span style="color:#000">Autowired</span>
    <span style="color:#000">private</span> <span style="color:#000">Redisson</span> <span style="color:#000">redisson</span><span style="color:#000;font-weight:bold">;</span>

    <span style="color:#a40000">@</span><span style="color:#000">PostMapping</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;/testLock11&#34;</span><span style="color:#000;font-weight:bold">)</span>
    <span style="color:#000">public</span> <span style="color:#000">ApiResult</span><span style="color:#000;font-weight:bold">&lt;</span><span style="color:#000">Boolean</span><span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#000">testLock11</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">String</span> <span style="color:#000">lockKey</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">String</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">format</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">FORMAT_LOCKKEY</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">3</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;-------lockKey:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lockKey</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">RLock</span> <span style="color:#000">lock</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">redisson</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getLock</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">lockKey</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;-------创建锁之后 isLocked-1:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
<span style="color:#8f5902;font-style:italic">//        try {
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//            Thread.sleep(10000);
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        } catch (InterruptedException e) {
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//            e.printStackTrace();
</span><span style="color:#8f5902;font-style:italic"></span><span style="color:#8f5902;font-style:italic">//        }
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000">Future</span><span style="color:#000;font-weight:bold">&lt;</span><span style="color:#000">Boolean</span><span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#000">res</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">tryLockAsync</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">10</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">30</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">TimeUnit</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SECONDS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;-------tryLockAsync 后 isLocked-2:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">try</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">Thread</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">sleep</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">10000</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span> <span style="color:#000">catch</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">InterruptedException</span> <span style="color:#000">e</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">e</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">printStackTrace</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;-------10秒 后----isLocked-3:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#8f5902;font-style:italic">//throw new BusinessException(&#34;测试获取锁后发生异常&#34;);
</span><span style="color:#8f5902;font-style:italic"></span>        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isHeldByCurrentThread</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;-------isHeldByCurrentThread:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isHeldByCurrentThread</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000">boolean</span> <span style="color:#000">result</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#204a87;font-weight:bold">false</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">try</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">result</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">res</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">get</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;-------result:&#34;</span> <span style="color:#ce5c00;font-weight:bold">+</span> <span style="color:#000">result</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">result</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">{</span>
                <span style="color:#000">Thread</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">sleep</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">10000</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isHeldByCurrentThread</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">{</span>
                    <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;-------isHeldByCurrentThread:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isHeldByCurrentThread</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#000;font-weight:bold">}</span>
            <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000;font-weight:bold">}</span> <span style="color:#000">catch</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">InterruptedException</span> <span style="color:#000">e</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">e</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">printStackTrace</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span> <span style="color:#000">catch</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">ExecutionException</span> <span style="color:#000">e</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">e</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">printStackTrace</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000;font-weight:bold">}</span><span style="color:#000">finally</span> <span style="color:#000;font-weight:bold">{</span>
            <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;-------666666-------unlock-isLocked:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">{</span>
                <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;-------88888888-------解锁解锁:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
                <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">unlock</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;res.get:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">result</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">ApiResult</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ok</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>

    <span style="color:#a40000">@</span><span style="color:#000">PostMapping</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;/testLock12&#34;</span><span style="color:#000;font-weight:bold">)</span>
    <span style="color:#000">public</span> <span style="color:#000">ApiResult</span><span style="color:#000;font-weight:bold">&lt;</span><span style="color:#000">Boolean</span><span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#000">testLock12</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span> <span style="color:#000;font-weight:bold">{</span>
        <span style="color:#000">String</span> <span style="color:#000">lockKey</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">String</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">format</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">FORMAT_LOCKKEY</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">3</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;====================lockKey:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lockKey</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">RLock</span> <span style="color:#000">lock</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">redisson</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">getLock</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">lockKey</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;====================isLocked-1:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">Future</span><span style="color:#000;font-weight:bold">&lt;</span><span style="color:#000">Boolean</span><span style="color:#000;font-weight:bold">&gt;</span> <span style="color:#000">res</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">tryLockAsync</span><span style="color:#000;font-weight:bold">(</span><span style="color:#0000cf;font-weight:bold">5</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#0000cf;font-weight:bold">2</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">TimeUnit</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">SECONDS</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">boolean</span> <span style="color:#000">locked</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isLocked</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;====================isLocked-2:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">locked</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">locked</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">{</span>
            <span style="color:#204a87;font-weight:bold">if</span> <span style="color:#000;font-weight:bold">(</span><span style="color:#000">lock</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">isHeldByCurrentThread</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">{</span>
                <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;====================锁住了，是我的锁&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000;font-weight:bold">}</span><span style="color:#204a87;font-weight:bold">else</span> <span style="color:#000;font-weight:bold">{</span>
                <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;====================锁住了，不是我的锁&#34;</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
            <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000;font-weight:bold">}</span>
        <span style="color:#000">Boolean</span> <span style="color:#000">getLock</span> <span style="color:#000;font-weight:bold">=</span> <span style="color:#000">null</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#000">log</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">info</span><span style="color:#000;font-weight:bold">(</span><span style="color:#4e9a06">&#34;====================getLock-2:{}&#34;</span><span style="color:#000;font-weight:bold">,</span> <span style="color:#000">getLock</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
        <span style="color:#204a87;font-weight:bold">return</span> <span style="color:#000">ApiResult</span><span style="color:#000;font-weight:bold">.</span><span style="color:#000">ok</span><span style="color:#000;font-weight:bold">(</span><span style="color:#000">locked</span><span style="color:#000;font-weight:bold">)</span><span style="color:#000;font-weight:bold">;</span>
    <span style="color:#000;font-weight:bold">}</span>

<span style="color:#000;font-weight:bold">}</span>
<span style="color:#000">复制代码</span></code></pre></div>
<p>上面的代码看着很乱，当时摸索着打的日志，为了更好的理解加锁与解锁机制。</p>

<p>在 <code>testLock11</code> 接口里面添加线程睡眠，模仿程序运行占用锁，这时可以在redis里面看到我们加锁的key：</p>

<p><img src="https://p3-juejin.byteimg.com/tos-cn-i-k3u1fbpfcp/ea71a213e16f420f83651510e6ff9690~tplv-k3u1fbpfcp-watermark.awebp" alt="image.png" /></p>

<p>刚开始一直没有找到key，后来才发现，线程运行完成后，自动释放了锁，后面把睡眠时间拉长，才找到redis里面的key。</p>

<p>后面在用到代码里面，对加锁和锁的判断使用如下：</p>

<ol>
<li>设置加锁的唯一标识</li>

<li><p>获取锁，并持有，做自己的业务逻辑</p>
<div class="highlight"><pre style="background-color:#f8f8f8;-moz-tab-size:4;-o-tab-size:4;tab-size:4"><code class="language-fallback" data-lang="fallback">// 尝试获取锁-异步，第一个时间表示:最多等待时间，第二个时间参数表示:上锁以后多久自动解锁
Future&lt;Boolean&gt; res = lock.tryLockAsync(5, 5, TimeUnit.SECONDS);
if (lock.isLocked() &amp;&amp; lock.isHeldByCurrentThread()){
// 处理业务逻辑
// 解锁
if (lock.isLocked()){
    lock.unlock();
}
if (!lock.isLocked()){
    log.info(&#34;解锁成功&#34;);
}
return;
}
复制代码</code></pre></div></li>
</ol>

<p>这里判断加锁我使用了：<code>lock.isLocked() &amp;&amp; lock.isHeldByCurrentThread()</code>，这样可以确保只有一个线程进入锁的部分。</p>

<p>释放锁的时候，又加了一个判断：<code>lock.isLocked()</code>，避免由于业务逻辑耗时超过锁的自动释放时间，在执行<code>lock.unlock();</code>时，如果锁已经释放，或者别的线程拿到锁了，当前线程释放锁会抛出异常：</p>

<ol>
<li>手动释放或者到期自动释放</li>
</ol>

<h1 id="2-踩坑异常">2. 踩坑异常</h1>

<h2 id="1-error-processing-condition-on-org-springframework-boot-autoconfigure-cache-s">1. Error processing condition on org.springframework.boot.autoconfigure.cache.S</h2>

<p>首次添加redisson依赖后，原来的redis配置不好使了，项目启动就报了上面的错。最后切换实例化<code>CacheManager</code></p>

<p>参考 <a href="https://link.juejin.cn?target=https%3A%2F%2Fwww.cnblogs.com%2Ftimeflying%2Fp%2F13323942.html">SpringBoot通过Cacheable注解完成redis缓存功能</a></p>

<p>redisson入门可参考：# <a href="https://link.juejin.cn?target=https%3A%2F%2Fwww.cnblogs.com%2Fyoucong%2Fp%2F13939478.html">SpringBoot整合Redisson(单机版)</a></p>

<p>集群版配置文件可参考：<a href="https://link.juejin.cn?target=https%3A%2F%2Fblog.csdn.net%2Fweixin_30195669%2Farticle%2Fdetails%2F112993320">redisson版本_SpringBoot整合Redisson(集群版)</a></p>

<h2 id="2-o-redisson-client-handler-commandsqueue-exception-occured-channel">2. o.redisson.client.handler.CommandsQueue : Exception occured. Channel:</h2>

<p>刚开始由于使用了旧版本的redisson，发现项目启动后不久，控制台抛出此异常</p>

<p>参考：</p>

<p><a href="https://link.juejin.cn?target=https%3A%2F%2Fwww.cnblogs.com%2Fjunge8618%2Fp%2F9241927.html">www.cnblogs.com/junge8618/p…</a></p>

<p><a href="https://link.juejin.cn?target=https%3A%2F%2Fwww.jianshu.com%2Fp%2Fa89dbefb8f74">www.jianshu.com/p/a89dbefb8…</a></p>

<p>切换redisson版本，解决此异常。</p>

<p>其他参考博客：</p>

<p><a href="https://link.juejin.cn?target=https%3A%2F%2Fwww.jianshu.com%2Fp%2F118fceb2194a">SpringBoot整合Redisson </a></p>

<p><a href="https://link.juejin.cn?target=https%3A%2F%2Fwww.cnblogs.com%2Ftechnologykai%2Fp%2F8658710.html">REDIS分布式锁REDISSON扩展</a> 这篇博客结合AOP整合了redisson锁，值得学习下。</p>

<p>锁的相关概念，理解：<a href="https://link.juejin.cn?target=https%3A%2F%2Fblog.csdn.net%2Fluokn1995%2Farticle%2Fdetails%2F108371863">分布式锁，redisson是如何解决死锁问题</a></p>

<p><a href="https://link.juejin.cn?target=https%3A%2F%2Fblog.csdn.net%2Fxxcupid%2Farticle%2Fdetails%2F88120581">Redisson（1）分布式锁——如何解决死锁问题</a></p>

<p><a href="https://link.juejin.cn?target=https%3A%2F%2Fwww.jianshu.com%2Fp%2Fcde0700f0128">使用Redisson实现分布式锁</a></p>

<p>如果翻到了最后，就再推荐一篇通俗易懂的博客: <a href="https://link.juejin.cn?target=https%3A%2F%2Fblog.csdn.net%2Fzyhlwzy%2Farticle%2Fdetails%2F90721481">Spring Boot整合Redis实现简单的分布式锁</a> <a href="https://link.juejin.cn?target=https%3A%2F%2Fwww.jianshu.com%2Fp%2Fd93228cd69e2">SpringBoot整合Redisson实现分布式锁</a></p>

    </div>
    <footer class="post-footer">
     

     <div class="post-nav">
    <div class="post-nav-next post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis7.0%E7%89%88%E6%9C%AC%E4%BB%8B%E7%BB%8D/" rel="next" title="">
        <i class="fa fa-chevron-left"></i> 
        </a>
    
    </div>

    <div class="post-nav-prev post-nav-item">
    
        <a href="http://www.shutdown.cn/post/redis%E4%B9%8Bvm%E6%9C%BA%E5%88%B6/" rel="prev" title="">
         <i class="fa fa-chevron-right"></i>
        </a>
    
    </div>
</div>
      
     
     
     






    </footer>
  </article>
</section>

          </div>
        </div>
        <div class="sidebar-toggle">
  <div class="sidebar-toggle-line-wrap">
    <span class="sidebar-toggle-line sidebar-toggle-line-first"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-middle"></span>
    <span class="sidebar-toggle-line sidebar-toggle-line-last"></span>
  </div>
</div>
<aside id="sidebar" class="sidebar">
  <div class="sidebar-inner">

    <section class="site-overview sidebar-panel  sidebar-panel-active ">
      <div class="site-author motion-element" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image"
        src="http://www.shutdown.cn/img/author.jpg"
        alt="不与天斗Domino" />
    <p class="site-author-name" itemprop="name">不与天斗Domino</p>
    <p class="site-description motion-element" itemprop="description"> 
        Programmer &amp; Architect</p>
</div>
      <nav class="site-state motion-element">
    <div class="site-state-item site-state-posts">
      <a href="http://www.shutdown.cn/post/">
        <span class="site-state-item-count">183</span>
        <span class="site-state-item-name">日志</span>
      </a>
    </div>
    <div class="site-state-item site-state-categories">    
        <a href="http://www.shutdown.cn/categories/">      
         
        <span class="site-state-item-count">15</span>
        
        <span class="site-state-item-name">分类</span>
        
        </a>
    </div>

    <div class="site-state-item site-state-tags">
        <a href="http://www.shutdown.cn/tags/">
         
        <span class="site-state-item-count">224</span>
        
        <span class="site-state-item-name">标签</span>
        </a>
    </div>
</nav>
      
      

      

      <div class="links-of-blogroll motion-element inline">
<script type="text/javascript" src="//rf.revolvermaps.com/0/0/8.js?i=&amp;m=0&amp;s=220&amp;c=ff0000&amp;cr1=ffffff&amp;f=arial&amp;l=33&amp;bv=35" async="async"></script>
</div>

    </section>
    
  </div>
</aside>

      </div>
    </main>
   
    <footer id="footer" class="footer">
      <div class="footer-inner">
        <div class="copyright" >
  <span itemprop="copyrightYear">  &copy; 
  2013 - 2023</span>
  <span class="with-love"><i class="fa fa-heart"></i></span>
  <span class="author" itemprop="copyrightHolder">天地维杰网</span>
  <span class="icp" itemprop="copyrightHolder"><a href="https://beian.miit.gov.cn/" target="_blank">京ICP备13019191号-1</a></span>
</div>
<div class="powered-by">
  Powered by - <a class="theme-link" href="http://gohugo.io" target="_blank" title="hugo" >Hugo v0.63.2</a>
</div>
<div class="theme-info">
  Theme by - <a class="theme-link" href="https://github.com/xtfly/hugo-theme-next" target="_blank"> NexT
  </a>
</div>


      </div>
    </footer>

    <div class="back-to-top">
      <i class="fa fa-arrow-up"></i>
      <span id="scrollpercent"><span>0</span>%</span>
    </div>
  </div>

  

<script type="text/javascript">
  if (Object.prototype.toString.call(window.Promise) !== '[object Function]') {
    window.Promise = null;
  }
</script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery/index.js?v=2.1.3"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/fastclick/lib/fastclick.min.js?v=1.0.6"></script> 
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/jquery_lazyload/jquery.lazyload.js?v=1.9.7"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.min.js?v=1.2.1"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/vendor/velocity/velocity.ui.min.js?v=1.2.1"></script>
<script src="http://www.shutdown.cn/js/vendor/ua-parser-js/dist/ua-parser.min.js?v=0.7.9"></script>

<script src="http://www.shutdown.cn/js/vendor/fancybox/jquery.fancybox.pack.js?v=2.1.5"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/utils.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/motion.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/affix.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/schemes/pisces.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/scrollspy.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/post-details.js"></script>
<script type="text/javascript" src="http://www.shutdown.cn/js/toc.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/bootstrap.js"></script>

<script type="text/javascript" src="http://www.shutdown.cn/js/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js"],
    jax: ["input/TeX", "output/HTML-CSS"],
    tex2jax: {
      inlineMath: [ ['$','$'] ],
      displayMath: [ ['$$','$$'] ],
      processEscapes: true
    },
    "HTML-CSS": { fonts: ["TeX"] }
  });
</script>
<script src='https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.5/MathJax.js?config=TeX-AMS-MML_HTMLorMML' async></script>
</body>
</html>